<template>
  <div class="app-container">
    <h2 style="text-align: center;">FAQ</h2>
    <el-button
      type="text"
      @click="dialogfaqFormVisible = true
              faq = {name: ''}
              faqId = ''">添加</el-button>

    <ul class="levelOneList">
      <li
        v-for="faq in faqs"
        :key="faq.id">
        <p>
          {{ faq.question + ': ' + faq.answer }}
          <span class="acts">
            <el-button type="text" @click="edit_faq(faq.id)">编辑</el-button>
            <el-button type="text" @click="remove_faq(faq.id)">删除</el-button>
          </span>
        </p>
      </li>
    </ul>

    <el-dialog :visible.sync="dialogfaqFormVisible" title="编辑单位">
      <el-form :model="faq" label-width="120px">
        <el-form-item label="问题">
          <el-input v-model="faq.question"/>
        </el-form-item>
        <el-form-item label="答案">
          <el-input v-model="faq.answer"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogfaqFormVisible = false">取 消</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import faq from '@/api/hr/faq'

export default {
  data() {
    return {
      faqId: '',
      faqs: [],
      saveBtnDisabled: false,
      dialogfaqFormVisible: false,
      faq: { // 一级分类对象
        name: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.get_faqs()
    },
    get_faqs() {
      faq.get_faqs().then(response => {
        this.faqs = response.data.faqs
      })
    },

    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.faqId) {
        this.save_faq()
      } else {
        this.update_faq()
      }
    },
    save_faq() {
      faq.add_faq(this.faq).then(response => {
        this.$message({
          type: 'success',
          message: '保存成功!'
        })
        this.helpSave()
      }).catch((response) => {
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },
    update_faq() {
      this.faq.faqId = this.faqId
      faq.update_faq(this.faq).then(response => {
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
        this.helpSave()
      }).catch((response) => {
        // console.log(response)
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },

    helpSave() {
      this.dialogfaqFormVisible = false// 如果保存成功则关闭对话框
      this.dialogfaqTwoFormVisible = false// 如果保存成功则关闭对话框
      this.get_faqs()// 刷新列表
      this.faq = {}
      this.saveBtnDisabled = false
    },
    edit_faq(id) {
      this.faqId = id
      this.dialogfaqFormVisible = true
      faq.get_faq(id).then(response => { this.faq = response.data.faq })
    },
    remove_faq(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return faq.delete_faq(id)
      }).then(() => {
        this.get_faqs()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: response.data.message
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.levelOneList{
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.levelOneList li{
  position: relative;
}
.levelOneList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.levelOneList .acts {
  float: right;
  font-size: 14px;
}
</style>
